#include "struct.h"

TEXT *getNum(char *Filename, int *len) {
    FILE *fp = fopen(Filename,"rb");
    TEXT *charFreq = NULL;
    unsigned char ch;
    int i, temp, freq[256] = {0}, index = 0;
    if(fp == NULL) {
      perror("can't open file!");
      exit(0);
    };

    /*
    ** calculate freq for every char
    */
    temp = fgetc(fp);
    while( !feof(fp) ) {
      freq[temp]++;
      if( freq[temp] == 1 ) {
        (*len)++;
      }
      temp = fgetc(fp);
    }

    charFreq = (TEXT *) calloc(sizeof(TEXT), *len);
    for ( i = 0; i < 256; i++ ) {
      if( freq[i] ) {
        charFreq[index].charbody = i;
        charFreq[index].freq = freq[i];
        index ++;
      }
    }
    fclose(fp);
    return charFreq;
}